
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/
	
#include "GRTCommonParameter.h"

#include <QLineEdit>
#include <QLabel>
#include <QGridLayout>
#include <QString>
#include <QGroupBox>

#include "GRTGeometry.h"

#include "GRTScene.h"
#include "GRTMemoryLeak.h"
//constructor
GRTCommonParameter::GRTCommonParameter(QWidget* parent) : QWidget(parent)
{
	//create the position widgets
	_groupPosition = new QGroupBox("Position", this);
	_labelPosX = new QLabel("x=", 0);
	_labelPosY = new QLabel("y=", 0);
	_labelPosZ = new QLabel("z=", 0);
	_lePosX = new QLineEdit();
	_lePosY = new QLineEdit();
	_lePosZ = new QLineEdit();
	QGridLayout* positionLayout = new QGridLayout();
	positionLayout->addWidget(_labelPosX, 1, 0, 1, 1);
	positionLayout->addWidget(_lePosX, 1, 1, 1, 1);
	positionLayout->addWidget(_labelPosY, 2, 0, 1, 1);
	positionLayout->addWidget(_lePosY, 2, 1, 1, 1);
	positionLayout->addWidget(_labelPosZ, 3, 0, 1, 1);
	positionLayout->addWidget(_lePosZ, 3, 1, 1, 1);
	_groupPosition->setLayout(positionLayout);

	//create the rotation's widgets
	_groupRotation = new QGroupBox("Rotation");
	_labelRotX = new QLabel("x=");
	_labelRotY = new QLabel("y=");
	_labelRotZ = new QLabel("z=");
	_leRotX = new QLineEdit();
	_leRotY = new QLineEdit();
	_leRotZ = new QLineEdit();
	QGridLayout* rotationLayout = new QGridLayout();
	rotationLayout->addWidget(_labelRotX, 0, 0, 1, 1);
	rotationLayout->addWidget(_leRotX, 0, 1, 1, 1);
	rotationLayout->addWidget(_labelRotY, 1, 0, 1, 1);
	rotationLayout->addWidget(_leRotY, 1, 1, 1, 1);
	rotationLayout->addWidget(_labelRotZ, 2, 0, 1, 1);
	rotationLayout->addWidget(_leRotZ, 2, 1, 1, 1);
	_groupRotation->setLayout(rotationLayout);

	//create the scale's widgets
	_groupScale = new QGroupBox("Scale");
	_labelScaX = new QLabel("x=");
	_labelScaY = new QLabel("y=");
	_labelScaZ = new QLabel("z=");
	_leScaX = new QLineEdit();
	_leScaY = new QLineEdit();
	_leScaZ = new QLineEdit();
	QGridLayout* scaleLayout = new QGridLayout();
	scaleLayout->addWidget(_labelScaX, 0, 0, 1, 1);
	scaleLayout->addWidget(_leScaX, 0, 1, 1, 1);
	scaleLayout->addWidget(_labelScaY, 1, 0, 1, 1);
	scaleLayout->addWidget(_leScaY, 1, 1, 1, 1);
	scaleLayout->addWidget(_labelScaZ, 2, 0, 1, 1);
	scaleLayout->addWidget(_leScaZ, 2, 1, 1, 1);
	_groupScale->setLayout(scaleLayout);

	//create the layout
	_layout = new QGridLayout(this);
	_layout->addWidget(_groupPosition, 0, 0, 1, 1);
	_layout->addWidget(_groupRotation, 1, 0, 1, 1);
	_layout->addWidget(_groupScale, 2, 0, 1, 1);
	setLayout(_layout);

	//connection to change the position of the object
	connect(_lePosX, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectPosition()));
	connect(_lePosY, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectPosition()));
	connect(_lePosZ, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectPosition()));

	//connection to change the rotation of the object
	connect(_leRotX, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectRotation()));
	connect(_leRotY, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectRotation()));
	connect(_leRotZ, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectRotation()));

	//connection to change the scale of the object
	connect(_leScaX, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectScale()));
	connect(_leScaY, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectScale()));
	connect(_leScaZ, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectScale()));
}

//destructor
GRTCommonParameter::~GRTCommonParameter(){}

//fill the widgets with values
void GRTCommonParameter::fillWidgets(GRTMovable* g)
{
	//set translation
	_lePosX->setText(QString::number(g->getTranslate().x));
	_lePosY->setText(QString::number(g->getTranslate().y));
	_lePosZ->setText(QString::number(g->getTranslate().z));

	//set rotation
	_leRotX->setText(QString::number(g->getRotate().x));
	_leRotY->setText(QString::number(g->getRotate().y));
	_leRotZ->setText(QString::number(g->getRotate().z));

	//set scale
	_leScaX->setText(QString::number(g->getScale().x));
	_leScaY->setText(QString::number(g->getScale().y));
	_leScaZ->setText(QString::number(g->getScale().z));
}

//set position
void GRTCommonParameter::setPosition(float x, float y, float z)
{
	//set translation
	_lePosX->setText(QString::number(x));
	_lePosY->setText(QString::number(y));
	_lePosZ->setText(QString::number(z));
}

//slot to set the rotation values of the widget
void GRTCommonParameter::setRotation(float x, float y, float z)
{
	_leRotX->setText(QString::number(x));
	_leRotY->setText(QString::number(y));
	_leRotZ->setText(QString::number(z));
}

//slot to set the scale values of the widget
void GRTCommonParameter::setScale(float x, float y, float z)
{
	_leScaX->setText(QString::number(x));
	_leScaY->setText(QString::number(y));
	_leScaZ->setText(QString::number(z));
}

//set the selected object to the position in the widget
void GRTCommonParameter::setObjectPosition()const
{
	GRTMovable* g = SCENE.getSelectedObject();
	if(g != 0)
		g->setTranslate(RTVector3f(_lePosX->text().toFloat(), _lePosY->text().toFloat(), _lePosZ->text().toFloat()));
}

//set the selectec object to the rotation in the widget
void GRTCommonParameter::setObjectRotation()const
{
	GRTMovable* g = SCENE.getSelectedObject();
	if(g != 0)
		g->setRotation(RTVector3f(_leRotX->text().toFloat(), _leRotY->text().toFloat(), _leRotZ->text().toFloat()));
}

//set the selectec object to the scale in the widget
void GRTCommonParameter::setObjectScale()const
{
	GRTMovable* g = SCENE.getSelectedObject();
	if(g != 0)
		g->setScale(RTVector3f(_leScaX->text().toFloat(), _leScaY->text().toFloat(), _leScaZ->text().toFloat()));
}